//
// Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
//
// GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
//  Author: Shawn.Tan
//  Date : 2025.10.28
//
//  History : Initial Version 2025.10.28
//
//
#include "RegisterInitData.h"

#include "Data.h"
#include "Enums.h"
#include "Generator.h"
#include "Generator.h"
#include "Log.h"
#include "OperandDataRequest.h"
#include "Register.h"
#include ARCH_ENUM_HEADER

using namespace std;
namespace Green {
  void RegisterInitData::Setup(const Generator& gen, const Register *pRegister , const OperandDataRequest *pDataRequest)
  {
    mpDataRequest = pDataRequest;
    if (mpDataRequest != nullptr) {
      auto valueStr = mpDataRequest->GetDataString();
      mpData = DataFactory::Instance()->BuildData(valueStr, gen);
    }
    else {
      mpData = DataFactory::Instance()->BuildDataPattern(pRegister, gen);
    }
  }

  void RegisterInitData::Commit(const Generator& gen, Register* pRegister)
  {
    auto reg_file = gen.GetRegisterFile();
    if (nullptr == mpDataRequest) {
      if (pRegister->RegisterType() == ERegisterType::SysReg) {
        gen.RandomInitializeRegister(pRegister);
        return;
      }
    }

    ChoicesModerator* choices_mod = nullptr;
    if (pRegister->RegisterType() == ERegisterType::SysReg) {
      choices_mod = gen.GetChoicesModerator(EChoicesType::RegisterFieldValueChoices);
    }

    auto reg_name = pRegister->Name();
    if (pRegister->IsLargeRegister()) {
      vector<uint64> datas;
      mpData->ChooseLargeData(datas);
      reg_file->InitializeRegister(reg_name, datas, choices_mod);
    }
    else {
      auto data = mpData->ChooseData();
      reg_file->InitializeRegister(reg_name, data, choices_mod);
      // << "{RegisterInitData::Commit} Register "<< reg_name << " is initialized to 0x" << std::hex << data << endl;
    }

    if (mpDataRequest != nullptr)
      mpDataRequest->SetApplied();

  }

  void RegisterInitData::Cleanup()
  {
    delete mpData;
  }

}
